<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addFn"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" align="center" type="index" width="120" />
          <el-table-column label="角色名称" align="center" width="240" prop="name" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="scope">
              <el-button size="small" type="primary">分配权限</el-button>
              <el-button size="small" type="success" @click="edit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="total"
            layout="prev,pager,next"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!--对话框  -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="dialogClose"
    >
      <el-form ref="roleRef" :model="roleForm" :rules="roleRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色信息">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRole, getRoleDetail, updateRole, delRole } from '@/api'
export default {
  data() {
    return {
      activeName: 'first',
      // 当前页数据
      list: [],
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      // 接口传值
      params: {
        page: 1,
        pagesize: 10
      },
      // name表单框校验
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      total: 0
    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    // 掉过接口 先created接受再在methods里封装函数调用
    this.fetchRoleList()
  },
  methods: {
    // 角色详情
    async edit(val) {
      this.currentNode = val
      const { data } = await getRoleDetail(val.id)
      this.roleForm = data
      this.dialogVisible = true
    },
    // 显示第几页发生变化
    handleCurrentChange(newPage) {
      // 将页码赋值当前的最新页码
      this.params.page = newPage
      // 获取最新的表格数据
      this.fetchRoleList()
    },
    // 渲染表单数据
    async fetchRoleList() {
      // 因为要分页所以加个this.params参数
      const { data } = await getRoleList(this.params)
      this.list = data.rows
      this.total = data.total
    },
    // 点击后显示弹窗
    addFn() {
      this.dialogVisible = true
    },
    // 提交事件
    async submit() {
      // 1. 兜底校验
      this.$refs.roleRef.validate(async valid => {
        if (!valid) return
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          // 调用接口
          await addRole(this.roleForm)
        }
        // 提示
        this.$message.success(this.title + '添加成功')
        // 关闭弹窗
        this.dialogClose()
        // 刷新列表
        this.fetchRoleList()
      })
    },
    // 弹窗关闭后，执行的回调
    dialogClose() {
      this.dialogVisible = false
      // 重置表单
      this.$refs.roleRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 点击删除
    del(val) {
      this.$confirm('确定删除吗', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(val.id)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.params.page !== 1) {
          this.params.page--
        }
        this.fetchRoleList()
      }).catch(() => {
        this.$message.error('取消删除')
      })
    }
  }
}

</script>
<style lang='less' scoped>
</style>
